/*
 * VentanaNuevaReceta.java
 *
 * Created on 18 de septiembre de 2008, 08:03
 */

package vista;

//import controlador.ControladorAlta;
//import controlador.ControladorConsulta;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import dominio.Ingrediente;
import dominio.Receta;
import javax.swing.JOptionPane;
import vista.util.VentanaNuevaRecetaUtil;

/**
 *
 * @author  marcelo
 */
public class VentanaNuevaReceta extends javax.swing.JFrame 
{
    private Collection elementos = new ArrayList();
    private Collection ingredientes = new ArrayList();
    private Collection recetas = new ArrayList();
    private Collection devolverRecetas = new ArrayList();
    

    private VentanaNuevaRecetaUtil util = new VentanaNuevaRecetaUtil();
    private VentanaNuevoPerfil nuevoPerfil;
    private boolean guardar = false;
    

    /** Creates new form VentanaNuevaReceta */
    public VentanaNuevaReceta() 
    {
        initComponents();
        this.inicializar();
    }

    public VentanaNuevaReceta(VentanaNuevoPerfil nuevoPerfil)
    {
        initComponents();
        this.inicializar();
        this.nuevoPerfil = nuevoPerfil;
        this.jButtonAgregarReceta.setEnabled(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableIngredientes = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabelCantidad = new javax.swing.JLabel();
        jTextFieldCantidad = new javax.swing.JTextField();
        jButtonAgregar = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jRadioButtonUnidades = new javax.swing.JRadioButton();
        jRadioButtonCucharadas = new javax.swing.JRadioButton();
        jRadioButtonKg = new javax.swing.JRadioButton();
        jRadioButtonTaza = new javax.swing.JRadioButton();
        jComboBoxIngrediente = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextAreaInstrucciones = new javax.swing.JTextArea();
        jButtonGuardar = new javax.swing.JButton();
        jButtonModificar = new javax.swing.JButton();
        jButtonNuevo = new javax.swing.JButton();
        jButtonSalir = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableRecetas = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextAreaNombre = new javax.swing.JTextArea();
        jLabelCantidad1 = new javax.swing.JLabel();
        jTextFieldCalorias = new javax.swing.JTextField();
        jTextFieldPorciones = new javax.swing.JTextField();
        jLabelCantidad2 = new javax.swing.JLabel();
        jTextFieldTiempoPreparado = new javax.swing.JTextField();
        jLabelCantidad3 = new javax.swing.JLabel();
        jLabelCantidad4 = new javax.swing.JLabel();
        jComboBoxCategoria = new javax.swing.JComboBox();
        jButtonAgregarReceta = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nueva Receta");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Ingredientes"));

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jTableIngredientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Cantidad"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableIngredientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableIngredientesMouseClicked(evt);
            }
        });
        jTableIngredientes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTableIngredientesKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(jTableIngredientes);

        jLabel2.setBackground(new java.awt.Color(255, 255, 204));
        jLabel2.setText("Alimento");
        jLabel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel2.setOpaque(true);

        jLabelCantidad.setBackground(new java.awt.Color(255, 255, 204));
        jLabelCantidad.setText("Cantidad");
        jLabelCantidad.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelCantidad.setOpaque(true);

        jTextFieldCantidad.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldCantidad.setEnabled(false);

        jButtonAgregar.setText("Agregar");
        jButtonAgregar.setEnabled(false);
        jButtonAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAgregarActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Medida"));

        buttonGroup1.add(jRadioButtonUnidades);
        jRadioButtonUnidades.setText("Unidades");

        buttonGroup1.add(jRadioButtonCucharadas);
        jRadioButtonCucharadas.setText("Cucharadas");

        buttonGroup1.add(jRadioButtonKg);
        jRadioButtonKg.setSelected(true);
        jRadioButtonKg.setText("Kg");

        buttonGroup1.add(jRadioButtonTaza);
        jRadioButtonTaza.setText("Taza");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButtonKg)
                    .addComponent(jRadioButtonCucharadas)
                    .addComponent(jRadioButtonUnidades)
                    .addComponent(jRadioButtonTaza))
                .addContainerGap(11, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jRadioButtonKg)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonCucharadas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonUnidades)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonTaza)
                .addContainerGap(10, Short.MAX_VALUE))
        );

        jComboBoxIngrediente.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxIngrediente, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxIngrediente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonAgregar))
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Procedimiento"));

        jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jTextAreaInstrucciones.setColumns(20);
        jTextAreaInstrucciones.setLineWrap(true);
        jTextAreaInstrucciones.setRows(5);
        jTextAreaInstrucciones.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextAreaInstrucciones.setEnabled(false);
        jScrollPane3.setViewportView(jTextAreaInstrucciones);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
        );

        jButtonGuardar.setText("Guardar");
        jButtonGuardar.setEnabled(false);
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonModificar.setText("Modificar");
        jButtonModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonModificarActionPerformed(evt);
            }
        });

        jButtonNuevo.setText("Nuevo");
        jButtonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevoActionPerformed(evt);
            }
        });

        jButtonSalir.setText("Salir");
        jButtonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalirActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Recetas"));

        jTableRecetas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", ""
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableRecetas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableRecetasMouseClicked(evt);
            }
        });
        jTableRecetas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTableRecetasKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTableRecetas);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Informacion de Receta"));

        jLabel3.setBackground(new java.awt.Color(255, 255, 204));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Nombre de la Receta");
        jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel3.setOpaque(true);

        jTextAreaNombre.setColumns(20);
        jTextAreaNombre.setRows(5);
        jTextAreaNombre.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextAreaNombre.setEnabled(false);
        jScrollPane4.setViewportView(jTextAreaNombre);

        jLabelCantidad1.setBackground(new java.awt.Color(255, 255, 204));
        jLabelCantidad1.setText("Calorias");
        jLabelCantidad1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelCantidad1.setOpaque(true);

        jTextFieldCalorias.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldCalorias.setEnabled(false);

        jTextFieldPorciones.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldPorciones.setEnabled(false);

        jLabelCantidad2.setBackground(new java.awt.Color(255, 255, 204));
        jLabelCantidad2.setText("Porciones");
        jLabelCantidad2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelCantidad2.setOpaque(true);

        jTextFieldTiempoPreparado.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldTiempoPreparado.setEnabled(false);

        jLabelCantidad3.setBackground(new java.awt.Color(255, 255, 204));
        jLabelCantidad3.setText("Tiempo de Preparado");
        jLabelCantidad3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelCantidad3.setOpaque(true);

        jLabelCantidad4.setBackground(new java.awt.Color(255, 255, 204));
        jLabelCantidad4.setText("Categoria");
        jLabelCantidad4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelCantidad4.setOpaque(true);

        jComboBoxCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DESAYUNO", "PLATO", "POSTRE" }));
        jComboBoxCategoria.setEnabled(false);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabelCantidad3, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldTiempoPreparado, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabelCantidad1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldCalorias, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addComponent(jLabelCantidad2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldPorciones, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabelCantidad4, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(50, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCantidad1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldCalorias, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldPorciones, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelCantidad2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCantidad3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldTiempoPreparado, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCantidad4, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jButtonAgregarReceta.setText("Agregar a Perfil");
        jButtonAgregarReceta.setEnabled(false);
        jButtonAgregarReceta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAgregarRecetaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonAgregarReceta, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButtonGuardar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jButtonNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(45, 45, 45))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonAgregarReceta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonNuevo)
                            .addComponent(jButtonModificar)
                            .addComponent(jButtonSalir))
                        .addGap(18, 18, 18)
                        .addComponent(jButtonGuardar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalirActionPerformed
// TODO add your handling code here:
//    this.ajustarTamanioColumna();
    this.dispose();
}//GEN-LAST:event_jButtonSalirActionPerformed

private void jButtonAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAgregarActionPerformed
// TODO add your handling code here:
    this.agregar();
}//GEN-LAST:event_jButtonAgregarActionPerformed

private void jButtonModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonModificarActionPerformed
// TODO add your handling code here:
    this.habilitarCampos();
    this.guardar = false;
    this.deshabilitarBotones();
}//GEN-LAST:event_jButtonModificarActionPerformed

private void jButtonNuevoActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonNuevoActionPerformed
{//GEN-HEADEREND:event_jButtonNuevoActionPerformed
    // TODO add your handling code here:
    this.ingredientes.clear();
    this.limpiar();
    this.habilitarCampos();
    this.guardar = true;
    this.deshabilitarBotones();
}//GEN-LAST:event_jButtonNuevoActionPerformed

private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonGuardarActionPerformed
{//GEN-HEADEREND:event_jButtonGuardarActionPerformed
    // TODO add your handling code here:
    this.habilitarBotones();
    this.deshabilitarCampos();
    if(this.guardar)
        this.guardarReceta();
    else
        this.modificar();
}//GEN-LAST:event_jButtonGuardarActionPerformed

private void jTableRecetasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableRecetasMouseClicked
    // TODO add your handling code here:
    this.mostrarCampos();
}//GEN-LAST:event_jTableRecetasMouseClicked

private void jTableIngredientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableIngredientesMouseClicked
    // TODO add your handling code here:
    this.mostrarIngrediente();
}//GEN-LAST:event_jTableIngredientesMouseClicked

private void jTableIngredientesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableIngredientesKeyPressed
    // TODO add your handling code here:
    if(evt.getKeyCode() == 127)
        this.quitarIngrediente();
}//GEN-LAST:event_jTableIngredientesKeyPressed

private void jTableRecetasKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_jTableRecetasKeyPressed
{//GEN-HEADEREND:event_jTableRecetasKeyPressed
    // TODO add your handling code here:
    if(evt.getKeyCode() == 127)
        this.borrarReceta();
}//GEN-LAST:event_jTableRecetasKeyPressed

private void jButtonAgregarRecetaActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonAgregarRecetaActionPerformed
{//GEN-HEADEREND:event_jButtonAgregarRecetaActionPerformed
    // TODO add your handling code here:
    this.devolver();
}//GEN-LAST:event_jButtonAgregarRecetaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaNuevaReceta().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonAgregar;
    private javax.swing.JButton jButtonAgregarReceta;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonModificar;
    private javax.swing.JButton jButtonNuevo;
    private javax.swing.JButton jButtonSalir;
    private javax.swing.JComboBox jComboBoxCategoria;
    private javax.swing.JComboBox jComboBoxIngrediente;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelCantidad;
    private javax.swing.JLabel jLabelCantidad1;
    private javax.swing.JLabel jLabelCantidad2;
    private javax.swing.JLabel jLabelCantidad3;
    private javax.swing.JLabel jLabelCantidad4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JRadioButton jRadioButtonCucharadas;
    private javax.swing.JRadioButton jRadioButtonKg;
    private javax.swing.JRadioButton jRadioButtonTaza;
    private javax.swing.JRadioButton jRadioButtonUnidades;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTableIngredientes;
    private javax.swing.JTable jTableRecetas;
    private javax.swing.JTextArea jTextAreaInstrucciones;
    private javax.swing.JTextArea jTextAreaNombre;
    private javax.swing.JTextField jTextFieldCalorias;
    private javax.swing.JTextField jTextFieldCantidad;
    private javax.swing.JTextField jTextFieldPorciones;
    private javax.swing.JTextField jTextFieldTiempoPreparado;
    // End of variables declaration//GEN-END:variables

    private void inicializar()
    {
        this.centrar();
        this.ajustarTamanioColumna();
        this.llenarComboElementos();
        this.llenarTablaRecetas();
    }
    private void centrar()
    {
        Dimension pantalla, cuadro;
	pantalla = Toolkit.getDefaultToolkit().getScreenSize();
	cuadro = this.getSize();
	this.setLocation(((pantalla.width - cuadro.width)/2), (pantalla.height - cuadro.height)/2);
    }

    private void llenarTablaRecetas()
    {
        this.recetas = this.util.traerTodos(this.jTableRecetas);
    }
    
    private void ajustarTamanioColumna()
    {
        TableColumn column = null;
        for (int i = 0; i < 3; i++)
        {
            column = jTableIngredientes.getColumnModel().getColumn(i);
            switch(i)
            {
                case 0:
                        column.setPreferredWidth(15);
//                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
                        break;
                case 1:
                        column.setPreferredWidth(182);
//                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
                        break;
                case 2:
                        column.setPreferredWidth(28);
//                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
                        break;
                case 3:
//                        column.setPreferredWidth(37);
                        break;
            }
        }
        
        for (int i = 0; i < 3; i++)
        {
            column = jTableRecetas.getColumnModel().getColumn(i);
            switch(i)
            {
                case 0:
                        column.setPreferredWidth(15);
//                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
                        break;
                case 1:
                        column.setPreferredWidth(280);
//                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
                        break;
                case 2:
                        column.setPreferredWidth(10);
//                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
                        break;
            }
        }
    }
    
    private void llenarComboElementos()
    {
        this.elementos = this.util.cargarComboElementos(this.jComboBoxIngrediente);
    }

    private void habilitarBotones()
    {
        this.jButtonModificar.setEnabled(true);
        this.jButtonNuevo.setEnabled(true);
    }

    private void deshabilitarBotones()
    {
        this.jButtonModificar.setEnabled(false);
        this.jButtonNuevo.setEnabled(false);
    }

    private void habilitarCampos()
    {
        this.jTextAreaInstrucciones.setEnabled(true);
        this.jTextAreaNombre.setEnabled(true);
        this.jTextFieldCalorias.setEnabled(true);
        this.jTextFieldCantidad.setEnabled(true);
        this.jTextFieldPorciones.setEnabled(true);
        this.jTextFieldTiempoPreparado.setEnabled(true);
        this.jButtonAgregar.setEnabled(true);
        this.jButtonGuardar.setEnabled(true);
        this.jComboBoxIngrediente.setEnabled(true);
        this.jComboBoxCategoria.setEnabled(true);
    }

    private void deshabilitarCampos()
    {
        this.jTextAreaInstrucciones.setEnabled(false);
        this.jTextAreaNombre.setEnabled(false);
        this.jTextFieldCalorias.setEnabled(false);
        this.jTextFieldCantidad.setEnabled(false);
        this.jTextFieldPorciones.setEnabled(false);
        this.jTextFieldTiempoPreparado.setEnabled(false);
        this.jButtonAgregar.setEnabled(false);
        this.jButtonGuardar.setEnabled(false);
        this.jComboBoxIngrediente.setEnabled(false);
        this.jComboBoxCategoria.setEnabled(false);
    }
    
    
    ////////////// AGREGAR ELEMENTOS A LA TABLA //////////////////
    private void agregar()
    {
        Ingrediente ingrediente = new Ingrediente();
        if(this.jRadioButtonCucharadas.isSelected())
        {
            ingrediente.setCucharadas(Integer.parseInt(this.jTextFieldCantidad.getText()));
            ingrediente.setSeleccion(0);
        }

        if(this.jRadioButtonKg.isSelected())
        {
            ingrediente.setPeso(Double.parseDouble(this.jTextFieldCantidad.getText()));
            ingrediente.setSeleccion(2);
        }

        if(this.jRadioButtonTaza.isSelected())
        {
            ingrediente.setTazas(Double.parseDouble(this.jTextFieldCantidad.getText()));
            ingrediente.setSeleccion(1);
        }

        if(this.jRadioButtonUnidades.isSelected())
        {
            ingrediente.setUnidades(Integer.parseInt(this.jTextFieldCantidad.getText()));
            ingrediente.setSeleccion(3);
        }

        this.ingredientes = this.util.agregarAlimento(jComboBoxIngrediente, ingrediente, elementos, ingredientes, this.jTableIngredientes);
        
    }

    private void quitarIngrediente()
    {
        this.ingredientes = this.util.quitarIngrediente(Integer.parseInt(String.valueOf(this.jTableIngredientes.getValueAt(this.jTableIngredientes.getSelectedRow(), 0))), this.ingredientes);
        this.util.cargarIngredientes(this.jTableIngredientes, this.ingredientes);
    }


    
    
    ////////// GUARDAR RECETAS /////////////////
    /**
     * Metodo para guardar las recetas cargadas por el usuario
     */
    private void guardarReceta()
    {
        if(this.verificarCampos())
        {
            this.util.guardarReceta(this.getReceta());
            this.limpiar();
            this.llenarTablaRecetas();
        }
        else
            JOptionPane.showMessageDialog(null, "Hay campos vacios, verifique el nombre de la receta o las instrucciones", "Faltan Datos", JOptionPane.ERROR_MESSAGE);
    }

    private void modificar()
    {
        if(this.verificarCampos())
        {
            Receta receta = this.getReceta();
            receta.setCodigo(Integer.parseInt(String.valueOf(this.jTableRecetas.getValueAt(this.jTableRecetas.getSelectedRow(), 0))));
            this.util.modificarReceta(receta);
            this.limpiar();
            this.llenarTablaRecetas();
        }
        else
            JOptionPane.showMessageDialog(null, "Hay campos vacios, verifique el nombre de la receta o las instrucciones", "Faltan Datos", JOptionPane.ERROR_MESSAGE);
    }

    private void borrarReceta()
    {
            Receta receta = this.getReceta();
            receta.setCodigo(Integer.parseInt(String.valueOf(this.jTableRecetas.getValueAt(this.jTableRecetas.getSelectedRow(), 0))));
            this.util.borrarReceta(receta);
            this.limpiar();
            this.llenarTablaRecetas();
    }

    private Receta getReceta()
    {
        Receta receta = new Receta();
        receta.setNombre(this.jTextAreaNombre.getText().trim().toUpperCase());
        receta.setInstrucciones(this.jTextAreaInstrucciones.getText().toUpperCase());
        try
        {
            receta.setCalorias(Integer.parseInt(this.jTextFieldCalorias.getText()));
        }
        catch(NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Debe cargar un numero en 'Calorias'", "Error de carga", JOptionPane.ERROR_MESSAGE);
        }
        try
        {
            receta.setPorciones(Integer.parseInt(this.jTextFieldPorciones.getText()));
        }
        catch(NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Debe cargar un numero en 'Porciones'", "Error de carga", JOptionPane.ERROR_MESSAGE);
        }
        receta.setIngrediente(this.ingredientes);
        receta.settiempoPreparado(this.jTextFieldTiempoPreparado.getText().trim().toUpperCase());
        receta.setCategoria(String.valueOf(this.jComboBoxCategoria.getSelectedItem()).trim().toUpperCase());
        return receta;
    }


    
    private boolean verificarCampos()
    {
        boolean bandera = true;
        if(this.jTextAreaNombre.getText().trim().length() == 0)
            bandera = false;
        if(this.jTextAreaInstrucciones.getText().trim().length() == 0)
            bandera = false;
        if(this.jTextFieldCalorias.getText().trim().length() == 0)
            bandera = false;
        if(this.jTextFieldPorciones.getText().trim().length() == 0)
            bandera = false;
        if(this.jTextFieldTiempoPreparado.getText().trim().length() == 0)
            bandera = false;
        return bandera;
    }

    private void limpiar()
    {
        //limpiar tabla ingredientes
        DefaultTableModel modelo = (DefaultTableModel)this.jTableIngredientes.getModel();
        while(this.jTableIngredientes.getRowCount() != 0)
                modelo.removeRow(0);
        modelo = null;
        //limpiar campos
        this.jTextAreaInstrucciones.setText("");
        this.jTextAreaNombre.setText("");
        this.jTextFieldCalorias.setText("");
        this.jTextFieldCantidad.setText("");
        this.jTextFieldPorciones.setText("");
        this.jTextFieldTiempoPreparado.setText("");
        this.jComboBoxCategoria.setSelectedIndex(0);
        this.jRadioButtonKg.setSelected(true);
        
    }

    private void mostrarCampos()
    {
        Receta receta = this.util.seleccionarReceta(Integer.parseInt(String.valueOf(this.jTableRecetas.getValueAt(this.jTableRecetas.getSelectedRow(), 0))),this.recetas);
        this.jTextAreaInstrucciones.setText(receta.getInstrucciones().trim());
        this.jTextAreaNombre.setText(receta.getNombre().trim());
        this.jTextFieldCalorias.setText(String.valueOf(receta.getCalorias()));
        this.jTextFieldPorciones.setText(String.valueOf(receta.getPorciones()));
        this.jTextFieldTiempoPreparado.setText(receta.gettiempoPreparado().trim());
        this.jComboBoxCategoria.setSelectedItem(receta.getCategoria().trim());
        this.ingredientes = receta.getIngrediente();
        this.util.cargarIngredientes(this.jTableIngredientes, this.ingredientes);
    }

    private void mostrarIngrediente()
    {
        Ingrediente i = this.util.getIngrediente(Integer.parseInt(String.valueOf(this.jTableIngredientes.getValueAt(this.jTableIngredientes.getSelectedRow(), 0))), this.ingredientes);
        switch(i.getSeleccion())
        {
            case 0:
                    this.jRadioButtonCucharadas.setSelected(true);
                    this.jTextFieldCantidad.setText(String.valueOf(i.getCucharadas()));
                    break;
            case 1:
                    this.jRadioButtonTaza.setSelected(true);
                    this.jTextFieldCantidad.setText(String.valueOf(i.getTazas()));
                    break;
            case 2:
                    this.jRadioButtonKg.setSelected(true);
                    this.jTextFieldCantidad.setText(String.valueOf(i.getPeso()));
                    break;
            case 3:
                    this.jRadioButtonUnidades.setSelected(true);
                    this.jTextFieldCantidad.setText(String.valueOf(i.getUnidades()));
                    break;
        }
        System.out.println("El nombre del ingrediente es: "+i.getElemento().getNombre().trim());
        this.jComboBoxIngrediente.setSelectedItem(i.getElemento().getNombre().trim());
    }

    private void devolver()
    {
        this.devolverRecetas = this.util.obtenerRecetas(this.jTableRecetas, this.recetas);
        this.nuevoPerfil.setRecetas(this.devolverRecetas);
        this.dispose();
    }


    
    
}
